package cn.edu.gdut.csq.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.edu.gdut.csq.entity.Companyinfo;
import cn.edu.gdut.csq.entity.PageSet;

@Component
public class CompanyDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public Session openSession() {
		return sessionFactory.openSession();
	}

	public Session getSession() {

		// 事务必须是开启的(Required)，否则获取不到

		return sessionFactory.getCurrentSession();

	}

	/**
	 * 按id 获取 公司
	 * 
	 * @param companyinfoId
	 * @return
	 */
	public Companyinfo getCompanyinfoById(Integer companyinfoId) {
		// TODO Auto-generated method stub

		String hql = "from Companyinfo company order by company.companyId desc";
		Query query = getSession().createQuery(hql);
		List<Companyinfo> companys = query.list();
		System.out.println(companys.size());

		if (companyinfoId > companys.size()) {
			return null;
		}
		return (Companyinfo) getSession().get(Companyinfo.class, companyinfoId);
	}

	/**
	 * 分页 获取 公司
	 * 
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	public PageSet<Companyinfo> getCompanyinfos(Integer pageNow,
			Integer pageSize, String queryType, Integer state) {
		// TODO Auto-generated method stub

		String hql = "from Companyinfo company where 1=1";

		System.out.println(queryType);
		if (!queryType.equals("list")) {

			if (queryType.equals("58tc")) {
				queryType = "58同城";

			} else if (queryType.equals("51job")) {
				queryType = "前程无忧";

			} else if (queryType.equals("zhaopin")) {
				queryType = "智联招聘";
			}

			hql += " and company.queryType ='" + queryType + "'";
		}

		if (state != -1) {
			hql += " and company.isnew =" + state;
		}

		hql += " order by company.companyId desc";

		System.out.println(hql);

		return this.getCompanyinfosBySql(pageNow, pageSize, hql);
	}

	/**
	 * 按时间 分页 获取 公司
	 * 
	 * @param pageNow
	 * @param pageSize
	 * @param time
	 * @param state
	 * @param queryType
	 * @return
	 */
	public PageSet<Companyinfo> getCompanyinfos(Integer pageNow,
			Integer pageSize, String time, String queryType, Integer state) {
		// TODO Auto-generated method stub
		String hql = "from Companyinfo company where company.queryTime like '"
				+ time + "%'";

		if (!queryType.equals("list")) {

			if (queryType.equals("58tc")) {
				queryType = "58同城";

			} else if (queryType.equals("51job")) {
				queryType = "前程无忧";

			} else if (queryType.equals("zhaopin")) {
				queryType = "智联招聘";
			}

			hql += " and company.queryType ='" + queryType + "'";
		}

		if (state != -1) {
			hql += " and company.isnew =" + state;
		}
		hql += " order by company.companyId desc";

		return this.getCompanyinfosBySql(pageNow, pageSize, hql);
	}

	/**
	 * 多条件 分页查询
	 * 
	 * @param pageNow
	 * @param pageSize
	 * @param sql
	 * @return
	 */
	public PageSet<Companyinfo> getCompanyinfosBySql(Integer pageNow,
			Integer pageSize, String hql) {
		// TODO Auto-generated method stub
		Query query = getSession().createQuery(hql)
				.setFirstResult((pageNow - 1) * pageSize)
				.setMaxResults(pageSize);
		List<Companyinfo> Companyinfos = query.list();
		PageSet<Companyinfo> pageCompanyinfos = new PageSet<Companyinfo>();
		pageCompanyinfos.setCurrentPage(pageNow);
		pageCompanyinfos.setList(Companyinfos);
		pageCompanyinfos.setNumPerPage(pageSize);
		Query query2 = getSession().createQuery(hql);
		int allRow = query2.list().size();
		pageCompanyinfos.setAllRow(allRow);
		pageCompanyinfos.setTotalPage(PageSet.countTotalPage(pageSize, allRow));
		System.out.println("totalpage" + pageCompanyinfos.getTotalPage());
		return pageCompanyinfos;
	}

	/**
	 * 按时间 导出 公司
	 * 
	 * @param time
	 * @param state
	 * @param queryType
	 * @return
	 */
	public List<Companyinfo> exportCompanyinfos(String time,
			String queryType, Integer state) {

		String hql = "from Companyinfo company where company.queryTime like '"
				+ time + "%'";

		if (!queryType.equals("list")) {

			if (queryType.equals("58tc")) {
				queryType = "58同城";

			} else if (queryType.equals("51job")) {
				queryType = "前程无忧";

			} else if (queryType.equals("zhaopin")) {
				queryType = "智联招聘";
			}

			hql += " and company.queryType ='" + queryType + "'";
		}

		if (state != -1) {
			hql += " and company.isnew =" + state;
		}
		hql += " order by company.companyId desc";

		Query query = getSession().createQuery(hql);
		List<Companyinfo> Companyinfos = query.list();
		return Companyinfos;
	}

	/**
	 * 更改状态
	 * 
	 * @param companyId
	 * @return
	 */
	public boolean changeState(Integer companyId) {
		String hql = "update Companyinfo company set company.isnew = 0 where company.companyId = "
				+ companyId;
		Query query = getSession().createQuery(hql);
		if (query.executeUpdate() != 0) {
			return true;
		}
		return false;
	}

}
